package com.smart.manager.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.system.entities.body.RSystemAccount;
import com.smart.manager.system.entities.models.SystemAccount;
import com.smart.manager.system.repository.AccountRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "账号管理")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "system/account")
@RequiredArgsConstructor
public class AccountApi {

    private final AccountRepository accountRepository;



    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询账号")
    @SaCheckPermission("system:account:pageList")
    public AjaxResult<Page<SystemAccount>> pageList(@Validated RSystemAccount.SearchBody body) {
        Page<SystemAccount> result = accountRepository.pageList(body);
        return AjaxResult.data(result);
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增账号")
    @SaCheckPermission("system:account:add")
    public AjaxResult<String> add(@RequestBody @Validated RSystemAccount.EditBody body) {
        String message = accountRepository.add(body);
        return AjaxResult.success(message);
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改账号")
    @SaCheckPermission("system:account:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RSystemAccount.EditBody body) {
        accountRepository.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除账号")
    @SaCheckPermission("system:account:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = accountRepository.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }



    @PutMapping(value = "resetPsw/{id}")
    @ApiOperation(value = "重置密码")
    @SaCheckPermission("system:account:resetPsw")
    public AjaxResult<String> resetPsw(@PathVariable Long id) {
      String msg=   accountRepository.resetPsw(id);
        return AjaxResult.success(msg);
    }

}
